//
//  MSFinanciaActivateCardController.m
//  觅尚
//
//  Created by No.8 on 2019/4/6.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSFinanciaActivateCardController.h"
#import "MSActivateCardCell.h"
#import "MSFinanciaActivateCardModel.h"

#define pageSize 20
@interface MSFinanciaActivateCardController ()<UITableViewDelegate,UITableViewDataSource>
//通用导航栏
{
    CustomNavigationBar *naviagtionBar;
    
    NSInteger page;
    
    NSInteger count;
}
/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;

/** 取反*/
@property(nonatomic,strong)UIButton *typeButton;

/** 遮盖层*/
@property(nonatomic,strong)UIView *backView;


/** 弹出View*/
@property(nonatomic,strong)UIView *showView;


/** 卡号*/
@property(nonatomic,strong)UITextField *CardNMField;


/** 金额*/
@property(nonatomic,strong)UITextField *MeneyField;


/** 用户昵称*/
@property(nonatomic,strong)UITextField *NameField;


/** 手机号*/
@property(nonatomic,strong)UITextField *TelField;


/** 数组*/
@property(nonatomic,strong)NSMutableArray *memberCardListArray;


/** 数组*/
@property(nonatomic,strong)UILabel *memberSummoney;

@end

@implementation MSFinanciaActivateCardController



/** Nav实现 */
- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
    self.view.backgroundColor = backgroundcolor;
    
    self.memberCardListArray = [NSMutableArray new];
    
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
    
}
- (void)MJChiBaoZiHeaderRefresh{
    page = 1;
    [self ReloadData];
}
- (void)MJChiBaoZiFootderRefresh{
    
    if (count / pageSize < page) {
        [MBManager showBriefAlert:@"没有更多数据了"];
    }else{
        page++;
    }
    [self FootderRefreshReloadData];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CreateNavigationBar];
    
    [self CreatView];
    
    [self tableView];
    
    PrepareFooter *footder = [PrepareFooter footerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiFootderRefresh)];
    // 马上进入刷新状态
    self.tableView.mj_footer = footder;
}
- (void)CreatView{
    
    UIView * HeaderView = [[UIView alloc] init];
    HeaderView.x = 0;
    HeaderView.x = 0;
    HeaderView.width = ScreenWidth;
    HeaderView.backgroundColor = backgroundcolor;
    
    //    self.CollectionHeaderView = HeaderView;
    
    /** 房间总量*/
    UILabel * totleCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(40 * px, 40 * px, ScreenWidth / 3 * 2, 60 * px)];
    totleCountLabel.textColor = [UIColor whiteColor];
//    totleCountLabel.text = [NSString stringWithFormat:@"今日总额     ￥%@",self.memberSummoney];
    totleCountLabel.textAlignment = NSTextAlignmentLeft;
    totleCountLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [HeaderView addSubview:totleCountLabel];
    self.memberSummoney = totleCountLabel;
    
    
    
    
    
    UIView * titleView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(totleCountLabel.frame) + 20 * px, HeaderView.width, 120 * px)];
    titleView.backgroundColor = BackViewColor;
    [HeaderView addSubview:titleView];
    NSArray * titleArray = @[@"日期",@"卡号",@"金额"];
    
    CGFloat CommoditytitleWidth = HeaderView.width / titleArray.count;
    CGFloat CommoditytitleY = 0;
    CGFloat CommoditytitleHeight = titleView.height;
    for (int index = 0; index < titleArray.count; index ++) {
        /** 标题label*/
        UILabel * Commoditytitle = [[UILabel alloc] init];
        Commoditytitle.x = CommoditytitleWidth * index;
        Commoditytitle.y = CommoditytitleY;
        Commoditytitle.width = CommoditytitleWidth;
        Commoditytitle.height = CommoditytitleHeight;
        Commoditytitle.textColor = WYCRGBColor(102, 121, 145);
        Commoditytitle.text = titleArray[index];
        Commoditytitle.textAlignment = NSTextAlignmentCenter;
        Commoditytitle.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
        [titleView addSubview:Commoditytitle];
    }
    
    HeaderView.height = CGRectGetMaxY(titleView.frame);
    [self.view addSubview:HeaderView];
    self.tableView.tableHeaderView = HeaderView;
    
    
    UIButton * bottomButton = [[UIButton alloc] initWithFrame:CGRectMake(0, ScreenHeight - 150 * px, ScreenWidth, 150 * px)];
    [bottomButton setTitle:@"开卡" forState:UIControlStateNormal];
    bottomButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [bottomButton setBackgroundImage:[UIImage imageNamed:@"底部按钮背景"] forState:UIControlStateNormal];
    [bottomButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bottomButton addTarget:self action:@selector(playCardButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bottomButton];
    
}

- (void)ReloadData{
    [self.memberCardListArray removeAllObjects];
    [MBManager showLoading];
    NSDictionary * params = @{@"busId":[WYCAccountTool unarchivebelongBusid],@"page":@(page),@"pageSize":@(pageSize)};
    [HTTPRequest POST:MSgetMemberCard params:params succeed:^(id responseObject) {
        WYCLog(@"---------%@",responseObject);
        if (CodeStr) {
            [self.tableView.mj_header endRefreshing];
            [MBManager hideAlert];
            count = [responseObject[@"data"][@"count"] integerValue];

            self.memberSummoney.text = [NSString stringWithFormat:@"今日总额:￥%@",responseObject[@"data"][@"memberSummoney"]];
            NSMutableArray *temporaryArray = [NSMutableArray new];
            temporaryArray = [MSFinanciaActivateCardModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"memberCardList"]];
            self.memberCardListArray  = temporaryArray;
        }else{
            [self.tableView.mj_header endRefreshing];
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page = [params[@"page"] integerValue];

        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
    }];
    
}

- (void)FootderRefreshReloadData{
    [self.memberCardListArray removeAllObjects];
    [MBManager showLoading];
    NSDictionary * params = @{@"busId":[WYCAccountTool unarchivebelongBusid],@"page":@(page),@"pageSize":@(pageSize)};
    [HTTPRequest POST:MSgetMemberCard params:params succeed:^(id responseObject) {
        WYCLog(@"---------%@",responseObject);
        if (CodeStr) {
            [self.tableView.mj_footer endRefreshing];
            [MBManager hideAlert];
            
            self.memberSummoney.text = [NSString stringWithFormat:@"今日总额:￥%@",responseObject[@"data"][@"memberSummoney"]];
            NSMutableArray *temporaryArray = [NSMutableArray new];
            temporaryArray = [MSFinanciaActivateCardModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"memberCardList"]];
            self.memberCardListArray  = temporaryArray;
        }else{
            [self.tableView.mj_footer endRefreshing];
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page --;
            
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
    }];
    
}
/** 表视图*/
- (UITableView *)tableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (!_tableView) {
        
        if (@available(iOS 11.0, *)) {
            
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
            
        }
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, [self navBarBottom], ScreenWidth, ScreenHeight - [self navBarBottom]) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.memberCardListArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MSActivateCardCell * cell = [MSActivateCardCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    cell.FinanciaActivateCardModel = self.memberCardListArray[indexPath.row];
    
    //无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"会员卡充值";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)playCardButton{
    
    UIView * view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [[UIApplication sharedApplication].keyWindow  addSubview:view];
    self.backView = view;
    
    
    UIView * showView = [[UIView alloc] init];
    showView.frame = CGRectMake(100 * px, ScreenHeight / 4 - 200 * px, ScreenWidth - 200 * px, ScreenHeight / 2 + 300 * px);
    showView.backgroundColor = [UIColor whiteColor];
    showView.layer.cornerRadius = 20 * px;
    [view addSubview:showView];
    self.showView = showView;
    
    
    UIButton * choseButton = [[UIButton alloc] init];
    choseButton.frame = CGRectMake(showView.width - 90 * px, 20 * px, 60 * px, 60 * px);
    [choseButton setImage:[UIImage imageNamed:@"pop_dis"] forState:UIControlStateNormal];
    [choseButton addTarget:self action:@selector(choseButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [showView addSubview:choseButton];
    
    
    /** 标题*/
    UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 80 * px, showView.width, 60 * px)];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.text = @"开卡";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont boldSystemFontOfSize:SYRealValue(35 / 2)];
    [showView addSubview:titleLabel];
    
    
    
    /** 标题*/
    UILabel * timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(40 * px, CGRectGetMaxY(titleLabel.frame) + 100 * px, 120 * px, 60 * px)];
    timeLabel.textColor = [UIColor blackColor];
    timeLabel.text = @"日期:";
    timeLabel.textAlignment = NSTextAlignmentLeft;
    timeLabel.font = [UIFont systemFontOfSize:SYRealValue(32 / 2)];
    [showView addSubview:timeLabel];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"YYYY-MM-dd"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    NSLog(@"currentTimeString =  %@",currentTimeString);
    UITextField * timeField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(timeLabel.frame) + 20 * px, timeLabel.y, showView.width - 260 * px, titleLabel.height)];
    timeField.text = [NSString stringWithFormat:@"%@",currentTimeString];
    [timeField setValue:[UIFont systemFontOfSize:SYRealValue(32 / 2)] forKeyPath:@"_placeholderLabel.font"];
    timeField.backgroundColor = [UIColor clearColor];
    timeField.textColor = [UIColor blackColor];
    [showView addSubview:timeField];
    
    
    UIView * timeLinView = [[UIView alloc] init];
    timeLinView.frame = CGRectMake(timeLabel.x, CGRectGetMaxY(timeField.frame) + 20 * px, showView.width - 80 * px, 1);
    timeLinView.backgroundColor = DividinglineColor;
    [showView addSubview:timeLinView];
    
    
    
    /** 标题*/
    UILabel * CardNMLabel = [[UILabel alloc] initWithFrame:CGRectMake(timeLabel.x, CGRectGetMaxY(timeLinView.frame) + 80 * px, timeLabel.width, timeLabel.height)];
    CardNMLabel.textColor = timeLabel.textColor;
    CardNMLabel.text = @"卡号:";
    CardNMLabel.textAlignment = timeLabel.textAlignment;
    CardNMLabel.font = timeLabel.font;
    [showView addSubview:CardNMLabel];
    
    
    UITextField * CardNMField = [[UITextField alloc] initWithFrame:CGRectMake(timeField.x, CardNMLabel.y, timeField.width, timeField.height)];
//    CardNMField.text = @"32222233";
    CardNMField.placeholder = @"请输入卡号";
    CardNMField.textAlignment = NSTextAlignmentRight;
    CardNMField.backgroundColor = timeField.backgroundColor;
    CardNMField.textColor = [UIColor blackColor];
    CardNMField.keyboardType = UIKeyboardTypeNumberPad;
    [showView addSubview:CardNMField];
    self.CardNMField = CardNMField;
    
    
    UIView * CardNMLinView = [[UIView alloc] init];
    CardNMLinView.frame = CGRectMake(timeLinView.x, CGRectGetMaxY(CardNMField.frame) + 20 * px, timeLinView.width, timeLinView.height);
    CardNMLinView.backgroundColor = DividinglineColor;
    [showView addSubview:CardNMLinView];
    
    
    
    /** 金额*/
    UILabel * MoneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(timeLabel.x, CGRectGetMaxY(CardNMLinView.frame) + 80 * px, timeLabel.width, timeLabel.height)];
    MoneyLabel.textColor = timeLabel.textColor;
    MoneyLabel.text = @"金额:";
    MoneyLabel.textAlignment = timeLabel.textAlignment;
    MoneyLabel.font = timeLabel.font;
    [showView addSubview:MoneyLabel];
    
    
    UITextField * MoneyField = [[UITextField alloc] initWithFrame:CGRectMake(timeField.x, MoneyLabel.y, timeField.width, timeField.height)];
//    MoneyField.text = @"￥1123344";
    MoneyField.placeholder = @"请输入金额";
    MoneyField.textAlignment = CardNMField.textAlignment;
    MoneyField.backgroundColor = timeField.backgroundColor;
    MoneyField.textColor = FontColor;
    MoneyField.keyboardType = UIKeyboardTypeNumberPad;
    [showView addSubview:MoneyField];
    self.MeneyField = MoneyField;
    
    
    UIView * MoneyLinView = [[UIView alloc] init];
    MoneyLinView.frame = CGRectMake(timeLinView.x, CGRectGetMaxY(MoneyField.frame) + 20 * px, timeLinView.width, timeLinView.height);
    MoneyLinView.backgroundColor = DividinglineColor;
    [showView addSubview:MoneyLinView];
    
    
    /** 金额*/
    UILabel * NameLabel = [[UILabel alloc] initWithFrame:CGRectMake(timeLabel.x, CGRectGetMaxY(MoneyLinView.frame) + 80 * px, timeLabel.width * 2, timeLabel.height)];
    NameLabel.textColor = timeLabel.textColor;
    NameLabel.text = @"用户名称:";
    NameLabel.textAlignment = timeLabel.textAlignment;
    NameLabel.font = timeLabel.font;
    [showView addSubview:NameLabel];
    
    
    UITextField * NameField = [[UITextField alloc] initWithFrame:CGRectMake(timeField.x, NameLabel.y, timeField.width, timeField.height)];
    //    MoneyField.text = @"￥1123344";
    NameField.placeholder = @"请输入用户名称";
    NameField.textAlignment = CardNMField.textAlignment;
    NameField.backgroundColor = timeField.backgroundColor;
    NameField.textColor = [UIColor blackColor];
    
    [showView addSubview:NameField];
    self.NameField = NameField;
    
    
    UIView * NameLinView = [[UIView alloc] init];
    NameLinView.frame = CGRectMake(timeLinView.x, CGRectGetMaxY(NameField.frame) + 20 * px, timeLinView.width, timeLinView.height);
    NameLinView.backgroundColor = DividinglineColor;
    [showView addSubview:NameLinView];
    
    
    
    
    /** 金额*/
    UILabel * TelLabel = [[UILabel alloc] initWithFrame:CGRectMake(timeLabel.x, CGRectGetMaxY(NameLinView.frame) + 80 * px, timeLabel.width * 2, timeLabel.height)];
    TelLabel.textColor = timeLabel.textColor;
    TelLabel.text = @"手机号:";
    TelLabel.textAlignment = timeLabel.textAlignment;
    TelLabel.font = timeLabel.font;
    [showView addSubview:TelLabel];
    
    
    UITextField * TelField = [[UITextField alloc] initWithFrame:CGRectMake(timeField.x, TelLabel.y, timeField.width, timeField.height)];
    //    MoneyField.text = @"￥1123344";
    TelField.placeholder = @"请输入用户手机号";
    TelField.textAlignment = CardNMField.textAlignment;
    TelField.backgroundColor = timeField.backgroundColor;
    TelField.textColor = [UIColor blackColor];
    TelField.keyboardType = UIKeyboardTypeNumberPad;
    [showView addSubview:TelField];
    self.TelField = TelField;
    
    
    UIView * TelLinView = [[UIView alloc] init];
    TelLinView.frame = CGRectMake(timeLinView.x, CGRectGetMaxY(TelField.frame) + 20 * px, timeLinView.width, timeLinView.height);
    TelLinView.backgroundColor = DividinglineColor;
    [showView addSubview:TelLinView];
    
    
    UIButton * completeButton = [[UIButton alloc] initWithFrame:CGRectMake(30 * px, CGRectGetMaxY(TelLinView.frame) + 80 * px, showView.width - 60 * px, 120 * px)];
    [completeButton setTitle:@"确定" forState:UIControlStateNormal];
    completeButton.layer.cornerRadius = 60 * px;
    completeButton.layer.masksToBounds = YES;
    completeButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [completeButton setBackgroundImage:[UIImage imageNamed:@"底部按钮背景"] forState:UIControlStateNormal];
    [completeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [completeButton addTarget:self action:@selector(completeButtonClick) forControlEvents:UIControlEventTouchUpInside];
//    [completeButton addTarget:self action:@selector(playCardButton) forControlEvents:UIControlEventTouchUpInside];
    [showView addSubview:completeButton];
    
}
-(void)choseButtonClick
{
    [self.backView removeFromSuperview];
}

- (void)completeButtonClick{
    
    if (self.CardNMField.text.length == 0 || self.MeneyField.text.length == 0 || self.TelField.text.length == 0) {
        [MBManager showBriefAlert:@"输入框不能为空"];
        return;
    }
    NSDictionary * params = @{@"memberNumber":self.CardNMField.text,@"memberMoney":self.MeneyField.text,@"busId":[WYCAccountTool unarchivebelongBusid],@"memberPhone":self.TelField.text};
    [HTTPRequest POST:MSaddMemberCard params:params succeed:^(id responseObject) {
        
        WYCLog(@"===========%@",responseObject);
        if (CodeStr) {
            [MBManager showBriefAlert:responseObject[@"msg"]];
            [self choseButtonClick];
            [self ReloadData];
        }else{
            
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
        
    } failure:^(NSError *error) {
        [MBManager showBriefAlert:Failed];
    }];
    
}
@end
